﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Bubblez
{
    public partial class frmoptions : Form
    {
        frmmain main = new frmmain();

        // Path Variables
        public string WoW_Path;
        public string WTF_Path;
        public string UI_Path;
        public string Filter_Path;
        public string Back_Path;
        public string Data_Path;
        public string Log_Path;

        // Search Engine
        public bool Search_Title;
        public bool Search_Author;
        public bool Search_Notes;
        public bool Search_Dependencies;
        public int Search_Mode;

        // Filter Options
        public string WildCard;
        public bool EnableExternalFilters;
        public bool EnableFiltersDuringSearch;
        public bool EnableDefaultFilters;

        public frmoptions()
        {
            InitializeComponent();
        }
        private void frmoptions_Load(object sender, EventArgs e)
        {
            if (File.Exists(main.Data_Path + "\\Settings.ini") == true)
            {
                string[] contents = File.ReadAllLines(main.Data_Path + "\\Settings.ini");
                string[] data;

                data = contents[0].Split('=');
                WoW_Path = data[1];
                data = contents[1].Split('=');
                WTF_Path = data[1];
                data = contents[2].Split('=');
                UI_Path = data[1];
                data = contents[3].Split('=');
                Filter_Path = data[1];
                data = contents[4].Split('=');
                Back_Path = data[1];
                data = contents[5].Split('=');
                Data_Path = data[1];
                data = contents[6].Split('=');
                Log_Path = data[1];
                data = contents[7].Split('=');
                WildCard = data[1];
                data = contents[8].Split('=');
                EnableExternalFilters = Convert.ToBoolean(data[1]);
                data = contents[9].Split('=');
                EnableFiltersDuringSearch = Convert.ToBoolean(data[1]);
                data = contents[10].Split('=');
                EnableDefaultFilters = Convert.ToBoolean(data[1]);
                data = contents[11].Split('=');
                Search_Title = Convert.ToBoolean(data[1]);
                data = contents[12].Split('=');
                Search_Author = Convert.ToBoolean(data[1]);
                data = contents[13].Split('=');
                Search_Notes = Convert.ToBoolean(data[1]);
                data = contents[14].Split('=');
                Search_Dependencies = Convert.ToBoolean(data[1]);
                data = contents[15].Split('=');
                Search_Mode = Convert.ToInt16(data[1]);
            }
            else
            {
                WoW_Path = main.WoW_Path;
                WTF_Path = main.WTF_Path;
                UI_Path = main.UI_Path;
                Filter_Path = main.Filter_Path;
                Back_Path = main.Back_Path;
                Data_Path = main.Data_Path;
                Log_Path = main.Log_Path;

                Search_Title = main.Search_Title;
                Search_Author = main.Search_Author;
                Search_Notes = main.Search_Notes;
                Search_Dependencies = main.Search_Dependencies;
                Search_Mode = main.Search_Mode;

                WildCard = main.WildCard;
                EnableExternalFilters = main.EnableExternalFilters;
                EnableDefaultFilters = main.EnableDefaultFilters;
                EnableFiltersDuringSearch = main.EnableFiltersDuringSearch;
            }

            txtBP.Text = Back_Path;
            txtFP.Text = Filter_Path;
            txtWP.Text = WoW_Path;
            txtwildcard.Text = WildCard;

            cmbMode.SelectedIndex = Search_Mode - 1;

            if (Search_Title == true)
                chkTitle.CheckState = CheckState.Checked;
            else
                chkTitle.CheckState = CheckState.Unchecked;
            if (Search_Author == true)
                chkAuthor.CheckState = CheckState.Checked;
            else
                chkAuthor.CheckState = CheckState.Unchecked;
            if (Search_Notes == true)
                chkNotes.CheckState = CheckState.Checked;
            else
                chkNotes.CheckState = CheckState.Unchecked;
            if (Search_Dependencies == true)
                chkDep.CheckState = CheckState.Checked;
            else
                chkDep.CheckState = CheckState.Unchecked;

            if (EnableExternalFilters == true)
                chkExFilters.CheckState = CheckState.Checked;
            else
                chkExFilters.CheckState = CheckState.Unchecked;
            if (EnableDefaultFilters == true)
                chkdefaultfilter.CheckState = CheckState.Checked;
            else
                chkdefaultfilter.CheckState = CheckState.Unchecked;
            if (EnableFiltersDuringSearch == true)
                chkfiltersearch.CheckState = CheckState.Checked;
            else
                chkfiltersearch.CheckState = CheckState.Unchecked;

        }
        private void Save_Settings()
        {
            string[] contents = new string[16];
            contents[0] = "WoW_Path=" + WoW_Path;
            contents[1] = "WTF_Path=" + WTF_Path;
            contents[2] = "UI_Path=" + UI_Path;
            contents[3] = "Filter_Path=" + Filter_Path;
            contents[4] = "Back_Path=" + Back_Path;
            contents[5] = "Data_Path=" + Data_Path;
            contents[6] = "Log_Path=" + Log_Path;

            contents[7] = "WildCard=" + WildCard;
            contents[8] = "EnableExternalFilters=" + Convert.ToString(EnableExternalFilters);
            contents[9] = "EnableFiltersDuringSearch=" + Convert.ToString(EnableFiltersDuringSearch);
            contents[10] = "EnableDefaultFilters=" + Convert.ToString(EnableDefaultFilters);

            contents[11] = "Search_Title=" + Convert.ToString(Search_Title);
            contents[12] = "Search_Author=" + Convert.ToString(Search_Author);
            contents[13] = "Search_Notes=" + Convert.ToString(Search_Notes);
            contents[14] = "Search_Dependencies=" + Convert.ToString(Search_Dependencies);
            contents[15] = "Search_Mode=" + Convert.ToString(Search_Mode);

            File.WriteAllLines(Data_Path + "\\Settings.ini", contents);
        }

        private void cmdApply_Click(object sender, EventArgs e)
        {
            Save_Settings();
        }
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cmdOk_Click(object sender, EventArgs e)
        {
            Save_Settings();
            this.Close();
        }

        private void cmdBP_Click(object sender, EventArgs e)
        {
            dirdiag.ShowDialog();
            if (dirdiag.SelectedPath != "")
            {
                txtBP.Text = dirdiag.SelectedPath;
                Back_Path = txtBP.Text;
            }
        }
        private void cmdFP_Click(object sender, EventArgs e)
        {
            dirdiag.ShowDialog();
            if (dirdiag.SelectedPath != "")
            {
                txtFP.Text = dirdiag.SelectedPath;
                Filter_Path = txtFP.Text;
            }
        }
        private void cmdWP_Click(object sender, EventArgs e)
        {
            dirdiag.ShowDialog();
            if (dirdiag.SelectedPath != "")
            {
                txtWP.Text = dirdiag.SelectedPath;
                WoW_Path = txtFP.Text;
            }
        }

        private void txtwildcard_TextChanged(object sender, EventArgs e)
        {
            WildCard = txtwildcard.Text;
        }

        private void chkTitle_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTitle.CheckState == CheckState.Checked)
                Search_Title = true;
            else
                Search_Title = false;
        }
        private void chkAuthor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAuthor.CheckState == CheckState.Checked)
                Search_Author = true;
            else
                Search_Author = false;
        }
        private void chkNotes_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNotes.CheckState == CheckState.Checked)
                Search_Notes = true;
            else
                Search_Notes = false;
        }
        private void chkDep_CheckedChanged(object sender, EventArgs e)
        {
            if (chkdefaultfilter.CheckState == CheckState.Checked)
                Search_Dependencies = true;
            else
                Search_Dependencies = false;
        }

        private void chkExFilters_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExFilters.CheckState == CheckState.Checked)
                EnableExternalFilters = true;
            else
                EnableExternalFilters = false;
        }
        private void chkfiltersearch_CheckedChanged(object sender, EventArgs e)
        {
            if (chkfiltersearch.CheckState == CheckState.Checked)
                EnableFiltersDuringSearch = true;
            else
                EnableFiltersDuringSearch = false;
        }
        private void chkdefaultfilter_CheckedChanged(object sender, EventArgs e)
        {
            if (chkdefaultfilter.CheckState == CheckState.Checked)
                EnableDefaultFilters = true;
            else
                EnableDefaultFilters = false;
        }

        private void cmbMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            Search_Mode = cmbMode.SelectedIndex + 1;
        }
    }
}
